home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Communications / Converse / Source / User.m < prev   
Text File  |  1995-12-28  |  4KB  |  176 lines

  1. //** Craig Laurent
  2. #import <sys/param.h>        //** for MAXHOSTNAMELEN constant
  3. #import <sys/types.h>        //** for getpwnam()
  4. #import <pwd.h>                //** for struct passwd
  5. #import "User.h"
  6.  
  7. @implementation User
  8.  
  9. - init
  10. {
  11.     if (self = [super init]) {
  12.         userID = nil;
  13.         userName = nil;
  14.         userMachine = nil;
  15.         return self;
  16.     }
  17.     return nil;
  18. }
  19.  
  20. - (void)dealloc
  21. {
  22.     if ([self userID])
  23.         [userID autorelease];
  24.     if ([self userName])
  25.         [userName autorelease];
  26.     if ([self userMachine])
  27.         [userMachine autorelease];
  28.     [super dealloc];
  29. }
  30.  
  31. //****************************************
  32. //** instance methods
  33. - (NSString *)userID
  34. {    return userID; }
  35. - (void)setUserID:(NSString *)newID
  36. {
  37.     [userID autorelease];
  38.     userID = [newID retain];
  39. }
  40.  
  41. - (NSString *)userName
  42. {    return userName; }
  43. - (void)setUserName:(NSString *)newName
  44. {
  45.     [userName autorelease];
  46.     userName = [newName retain];
  47. }
  48.  
  49. - (NSString *)userMachine
  50. {    return userMachine; }
  51. - (void)setUserMachine:(NSString *)newMachine
  52. {
  53.     [userMachine autorelease];
  54.     userMachine = [newMachine retain];
  55. }
  56.  
  57.  
  58. //****************************************
  59. //** method types
  60. /* loadUser - load User information from Unix system */
  61. - (void)loadUser
  62. {
  63.     char tempName[MAXHOSTNAMELEN];
  64.     struct passwd *passwordInfo;
  65.  
  66.     //** get User Information from Unix
  67.     passwordInfo = getpwuid(getuid());
  68.     [self setUserID:[NSString stringWithCString:passwordInfo->pw_name]];
  69.     [self setUserName:[NSString stringWithCString:passwordInfo->pw_gecos]];
  70.  
  71.     //** get Machine Name from Unix
  72.     gethostname(tempName, MAXHOSTNAMELEN);
  73.     [self setUserMachine:[NSString stringWithCString:tempName]];
  74. }
  75.  
  76. /* userNicknameWithID:... - returns a string with the pieces of user information that this user wants represented. */
  77. - (NSString *)userNicknameWithID:(BOOL)ID andName:(BOOL)name andMachine:(BOOL)machine
  78. {
  79.     NSMutableString *nickname;
  80.     int arguments = 0;
  81.  
  82.     nickname = [[[NSMutableString alloc] init] autorelease];
  83.  
  84.     //** create nickname
  85.     //** add user's ID first
  86.     if (ID && [self userID]) {
  87.         [nickname appendString:[self userID]];
  88.         arguments++;
  89.     }
  90.  
  91.     //** add user's machine name (after an "at" sign)
  92.     if (machine && [self userMachine]) {
  93.         [nickname appendString:@"@"];
  94.         [nickname appendString:[self userMachine]];
  95.         arguments++;
  96.     }
  97.  
  98.     //** add user's Real Name  last
  99.     //**  use parenthesis if not the only argument in the nickname
  100.     if (name && [self userName]) {
  101.         if (arguments > 0) {
  102.             [nickname appendString:@" ("];
  103.             [nickname appendString:[self userName]];
  104.             [nickname appendString:@")"];
  105.         } else
  106.             [nickname appendString:[self userName]];
  107.     }
  108.  
  109.     return nickname;
  110. }
  111.  
  112.  
  113. //****************************************
  114. //** data archiving methods
  115. /* encodeWithCoder: - encode User info for writing to file. */
  116. - (void)encodeWithCoder:(NSCoder *)aCoder
  117. {
  118.     [super encodeWithCoder:aCoder];
  119.     [aCoder encodePropertyList:[self userID]];
  120.     [aCoder encodePropertyList:[self userName]];
  121.     [aCoder encodePropertyList:[self userMachine]];
  122. }
  123.  
  124. /* initWithCoder: - decode User info after reading from file. */
  125. - initWithCoder:(NSCoder *)aDecoder
  126. {
  127.     [super initWithCoder:aDecoder];
  128.     [self setUserID:[aDecoder decodePropertyList]];
  129.     [self setUserName:[aDecoder decodePropertyList]];
  130.     [self setUserMachine:[aDecoder decodePropertyList]];
  131.  
  132.     return self;
  133. }
  134.  
  135.  
  136. //****************************************
  137. //** Distributed Object encoding methods
  138. /* encodeRemotelyFor:.. -  Distributed Object method to indicate what should be passed, The object or a copy.  NSObject won't be passed as a copy. */
  139. - encodeRemotelyFor:(NXConnection *)connection freeAfterEncoding:(BOOL *)flagp isBycopy:(BOOL)isBycopy
  140. {
  141.     if (isBycopy)
  142.         return self;    //** return copy of object, not a proxy
  143. //    return nil;    //** NSObject doesn't encode for DO.
  144.     return [super encodeRemotelyFor:connection freeAfterEncoding:flagp isBycopy:isBycopy];
  145. }
  146.  
  147. /* encodeUsing: -  Distributed Object method to encode the data for transfer */
  148. - encodeUsing:(id <NXEncoding>)portal
  149. {
  150.     [portal encodeData:&userID ofType:"@"];
  151.     [portal encodeData:&userName ofType:"@"];
  152.     [portal encodeData:&userMachine ofType:"@"];
  153.     return self;
  154. }
  155.  
  156. /* decodeUsing: -  Distributed Object method to decode the data after transfer */
  157. - decodeUsing:(id <NXDecoding>)portal
  158. {
  159.     self = [[self init] autorelease];
  160.  
  161.     //** decode data
  162.     [portal decodeData:&userID ofType:"@"];
  163.     [portal decodeData:&userName ofType:"@"];
  164.     [portal decodeData:&userMachine ofType:"@"];
  165.  
  166.     //** retain data
  167.     [userID retain];
  168.     [userName retain];
  169.     [userMachine retain];
  170.  
  171.     return self;
  172. }
  173.  
  174.  
  175. @end
  176.